#!/bin/bash

FILE_COUNT=$#
if((FILE_COUNT!=2)); then
    echo ">>> 错误：未传递文件参数！"
    echo "  用法：mysql_yum.sh rootPassword【root用户密码】 serverId【服务器编号】"
    exit;
fi

#指定用户名
MYSQL_ROOT_PASSWORD=$1
#服务Id
SERVER_ID=$2

if [ "$(command -v mysql)" ]; then
    echo "------------------ mysql已经安装 -------------------"
else
    echo "------------------ mysql准备开始安装 -------------------"
    wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
    yum -y install mysql57-community-release-el7-10.noarch.rpm
    rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
    yum -y install mysql-community-server 
    systemctl start  mysqld.service
    echo "------------------ mysql安装完成 -------------------"
fi
echo "------------------ 修改mysql配置 -------------------"
cat >/etc/my.cnf << EOF
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

# ---------------- mysql主从 --------------------
log-bin=mysql-bin #开启二进制日志
server-id=$SERVER_ID #服务id，不可重复
#log-error=/var/log/mysql/error.log
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
binlog_format=MIXED
#要给从机同步的库
# binlog-do-db=dbname 
#不给从机同步的库(多个写多行)
binlog-ignore-db=mysql 
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
#自动清理 7 天前的log文件,可根据需要修改
#expire_logs_days=7 
# 当开启二进制日志后，如果变量log_bin_trust_function_creators为OFF，那么创建或修改存储函数就会报“ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)”这样的错误
log_bin_trust_function_creators=TRUE
# 验证密码
validate_password_policy=LOW
auto_increment_offset=$SERVER_ID
auto_increment_increment=2
EOF

cat /etc/my.cnf
systemctl restart mysqld.service
systemctl status mysqld.service
# grep "password is generated for" /var/log/mysqld.log
PASSWORD_INIT=`grep "password is generated for" /var/log/mysqld.log | awk -F ': ' '{print $2}'`
echo "初始化数据库密码：$PASSWORD_INIT"
mysql -uroot -p$PASSWORD_INIT
# 参考初始化数据库密码：yBa8qENY-JZJ
# https://www.cnblogs.com/yss818824/p/12349719.html
# https://blog.csdn.net/qq_36582604/article/details/80526287
# https://www.cnblogs.com/lelehellow/p/9633315.html

# $PASSWORD_INIT << EOF
# # root用户
# set global validate_password_policy=LOW;
# alter user 'root'@'localhost' identified by '123456789';
# use mysql;
# update user set host='%' where user='root' and host='localhost';
# grant all privileges on *.* to 'root'@'%' identified by '123456789' with grant option;
# flush privileges;

# # 同步用户
# create user 'replic'@'%' identified by '$MYSQL_ROOT_PASSWORD';
# grant replication slave on *.* to 'replic'@'%' identified by '$MYSQL_ROOT_PASSWORD' with grant option;
# flush privileges;
# select host,user from mysql.user;

# SHOW MASTER STATUS;
# CHANGE MASTER TO
# MASTER_HOST='192.168.56.102',
# MASTER_USER='root',
# MASTER_PASSWORD='123456789',
# MASTER_LOG_FILE='mysql-bin.000003',
# MASTER_LOG_POS=154;
# start slave;
# show slave status\G;
